Variables and Operators
Variables
Variables are at the heart of any programming language. Salsa's variable system is designed to be flexible, robust, and convenient. Unlike many procedural languages like C and Pascal, you do not have to declare your variables before you use them. They are not typed, which means that "a", for example, is not necessarily an integer, string, or list, it is whatever you use it for. For example, the following code sets "a" to the number 6, and prints its value on the screen using the println command used in the Hello World! example:
def main()
{
a = 6;
println(a);
}
To assign a value to a variable, simple type the variable name and value on either sides of an equal sign as shown in the example. You can change the value in the same manner. The new value can be anything, no matter what the variable already holds. Therefore,
def main()
{
a = 6;
println(a);
a = -14.6;
println(a);
}
work like you would expect, even though you are changing the type of data a holds from an integer to a real.
Types
There are several types of variables you can have: integer, real, string, and list. An integer type holds a mathematical integer in the range -2^31..2^31-1, and you type it just as it looks. For example, these are all integers: 4 1523 -6 0. A real type holds a mathematical real number whose range depends on the computer you are running on, but is generally -10^309..10^309, and from 10 to 19 significant digits. Like integers, reals are typed exactly the way they normally look, although all must contain a decimal point to indicate they are reals rather then integers. For example, all of these are reals: 1.5 0.2 -0.7 5. 1234.5678. Note that 0.2 must be written with the zero; .2 is not correct. The string type is used to store text. To use it, simply include the text in double quotes. You may use C-Style escape codes in your strings. The list type is a powerful way to group values in a list. To type one, use brackets to enclose the elements, and separate them with commas. You may mix types in lists. For example, [2,3.3,"hi"] is a list of an integer, real, and string. [ [1,2],[3,4] ] is a list of two lists, and represents the matrix:
Operators
Just as no program can work without the help of variables, all programs will need operators to manipluate data. Operators are so called because they cause the computer to "operate" on data. Examples are the + operator for addition and the * operator for multiplication. You use operators in the normal mathematical way. For example, the following program puts an integer into two variables, and then prints their sum:
def main()
{
a = 3;
b = 4;
println(a+b);
}
Salsa's operators include the standard mathematical operators + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation). These operate on integers, reals, or a combination. The type returned matches the input types. That is, adding integers yields an integer, adding reals yields a real, and adding one of each yields a real. Dividing by zero results in an error. You have already seen the = operator which gives a variable a value. There are many other operators which will be convered in other sections.
Some question arises with order of precedence. This is the rule by which operators are evaluated when they come in a row. For example, in 2+2*2, mathematics dictates that the 2*2 be done first, and then 2 is added giving a result of 6. However, just evaluating left to right, we compute 2+2 and then multiply by 2 giving 8. Salsa uses the standard mathematical rules of precedence when evaluating expressions, so first exponentiation, then multiplication and division, and then addition and subtraction. In cases such as 1+2+3+4-5 where some or all the operators have equal precedence, the order is left to right.
What happens if you try to use operators on different types, like subtracting a string from an integer or adding a real to a list? As already mentioned, integers and reals can be used together without error. Some combinations will result in failure, since there is really no meaning in, for example, subtracting a string from an integer. But many combinations are defined. For example, adding anything to a list adds it to every element in the list. Multiplying two lists results in a "dot product," treating the lists as vectors. Multiplying two matricies (lists of lists which form a rectangular matrix) results in matrix multiplication. More on operating with lists on the Lists page. Adding anything to a string causes it to be attached to it, e.g. "testing"+1 yields "testing1". More on operating with strings on the Strings page.
The next section of the Tutorial describes control structures such as looping and if statements.
Web page maintained by Jason Cohen